﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Whatzup.Mobi.Common;
using Whatzup.Mobi.AppSvc.Provider;

namespace Whatzup.Mobi.AppSvc
{
    public class ServiceLocator : IServiceLocator
    {
        public T GetService<T>()
        {
            Type type = typeof(T);

            if (type == typeof(IMediaRepo))
                return (T)CreateMediaRepo();
            if (type == typeof(IMediaProvider))
                return (T)CreateMediaProvider();

            throw new ArgumentOutOfRangeException(type.ToString());
        }

        /// <summary>
        /// Lazy loading using null-coalescing.
        /// </summary>
        IMediaRepo m_mediaRepo;
        IMediaRepo CreateMediaRepo()
        {
            return m_mediaRepo ?? (m_mediaRepo = new MediaRepo(this));
        }

        /// <summary>
        /// Lazy loading using null-coalescing.
        /// </summary>
        IProvider m_mediaProvider;
        IProvider CreateMediaProvider()
        {
            return m_mediaProvider ?? (m_mediaProvider = new MediaProvider());
        }
    }
}